package wh.iolists;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import org.json.JSONException;

import wh.*;
import wh.exceptions.BadIDFormat;
import wh.exceptions.IDCollisionException;
import wh.exceptions.InvalidValueException;
import wh.myjson.*;

/**
 * Datastructures and functionality for customer details
 * @author Geir
 *
 */
public class CustomerList 
{
	private static ArrayList<Customer> customerList = new ArrayList<Customer>();
	private static CustomerList instance = null; // Singleton purpose
	
	////////Singleton part Begin //////////////
	
	protected CustomerList() {
		// Exists only to defeat instantiation.
	}

	public static CustomerList getInstance() {

		if (instance == null)
		{
			instance = new CustomerList();
		}
		return instance;
	}
	///////// Singleton part End ////////////////
	
	public static boolean findCustomerID(String idRef) 
	{
		boolean hasID = false;
		
		for (Customer cu : customerList)
		{
			if (cu.getCustomerID().equals(idRef)) {
				
				return true;
			}
		}
		return hasID;
	}
	
	/**
	 * Loads customers from Customers.txt
	 */
	public void restoreCustomers()
	{
		try {
            
            FileReader fileReader = new FileReader(new File("Customers.txt"));
            BufferedReader buffR = new BufferedReader(fileReader);
            
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = buffR.readLine()) != null) {
                sb.append(line);
            }
            
            JACustomer ja = new JACustomer(sb.toString());
            
            for (int i = 0; i < ja.length(); i++) {
                
            	JSONCustomer jo = new JSONCustomer(ja.get(i).toString());
            	
            	//Customer cust = createCustomer(jo);
            	customerList.add(createCustomer(jo));
            }
            
        } catch (JSONException ex) {
            
        } catch (FileNotFoundException ex) {
            
        } 
        catch (IOException ex) {
            
        }
		System.out.println("amount of customers: "+customerList.size());
	}
	
	public synchronized Customer getCustomer(String cID)
	{
		for (Customer customer : customerList)
		{
			if (customer.getCustomerID().equals(cID)) {
				
				return customer;
			}
		}
		return null;
	}
	
	private Customer createCustomer(JSONCustomer jo)
	{
		Customer c = null;
		try {
			c = new Customer(jo.getString("cID"), jo.getString("name"), jo.getString("cType"));
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (BadIDFormat e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IDCollisionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidValueException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return c;
	}
}
